/* 
*@module: Echart_Controller
*@module: provide echarts data(ground) for src/views/echart/index.vue
*@author:刘千红
*/
const AllEchartDataModel = require('../../models/echarts/echart_ground.js')
const AllMap = require('../../models/common/Map.js')
const assess_type_Map = new Map([[1, '初次调查'], [2, '详细调查'], [3, '修复调查']])

var element_Map

AllMap.then(data => {
  element_Map = data.element_Map
})

class echart_Controller {

  /**
   *获取ground柱状图数据
   *
   * @static
   * @param {*} ctx
   * @memberof echart_Controller
   */
  static async getHistogramData(ctx) {

    let SingleElementOptions = ctx.request.body //从前端传入的数据
    let { point_num, element, assess_type } = SingleElementOptions //将传入的数据将进行定义
    let res = await AllEchartDataModel.getHistogramElement(point_num, element)//通过point_num,assess_type,element将数据库中的数据取出


    let assess_type1 = []
    assess_type1.push(Number(assess_type))

    let DifferentDepthData = res.map(item => {
      return item = item.dataValues
    })
    console.log(111111111, DifferentDepthData)

    let NewValue = []
    DifferentDepthData.forEach((item) => {
      if (item["assess_type"] <= assess_type) {
        NewValue.push(item)
      }
    })
    console.log(22222, NewValue)

    let Type = res.map(item => {
      return item = item.dataValues.assess_type
    })//从数据库中取出的调查类型的数据

    Type = Type.filter((item, index, self) => {
      return index == self.indexOf(item)
    })
    //将得到的调查类型进行去重

    let newArr_Type = [];
    for (let i = 0; i < Type.length; i++) {
      for (let j = 0; j < assess_type1.length; j++) {
        if (assess_type1[j] >= Type[i]) {
          newArr_Type.push(Type[i]);
        }
      }
    }

    let TypeList = []
    newArr_Type.forEach((item) => { //let in 遍历数组元素即一个包含id,date,point_num还有各种元素值的对象，然后把这些元素对应的值，参考值，时间，是否受污染push到HistoryLists数组中
      if (item && assess_type_Map.has(item)) {
        TypeList.push(assess_type_Map.get(item))
      }
    })

    let mean_value1 = []
    let mean_value2 = []

    NewValue.forEach((item) => {

      if (item['statistic_value'] == 'mean_value') {

        mean_value1.push(item[element])

      } else if (item['statistic_value'] == 'max_value') {

        mean_value2.push(item[element])

      }
    })//将不同深度的想对应元素的数据取出放入一个数组中
    let ElementList = []
    ElementList.push({ assess_type: TypeList }, { mean_value: ['平均值', '最大值'] }, { mean_value: '平均值', value: mean_value1 }, { max: '最大值', value: mean_value2 })

    ctx.body = {
      success: true,
      res: ElementList,
      msg: '获取成功'
    }
  };

  /**
   *对应于ground 雷达图里面的在一个调查类型下的一个监测点位中的不同深度的数据值
   *
   * @static
   * @param {*} ctx
   * @memberof echart_Controller
   */
  static async getRadarEachDepthValue(ctx) {
    let FrontElement = ctx.request.body //从前端传入的数据
    // let { point_num,assess_type,reference_num,type} = FrontElement//将传入的数据将进行定义
    let { point_num, assess_type, type } = FrontElement//将传入的数据将进行定义
    let res = await AllEchartDataModel.getData(point_num, assess_type)
    let resDatar_arr = await AllEchartDataModel.getAllRadarData(type)
    let Datar_arr = resDatar_arr.map(item => {
      return item = item.dataValues
    })
    let Threshold17 = []
    let Threshold18 = []

    Datar_arr.forEach((item) => {

      if (item.reference_num == '17国标') {
        delete item.reference_num;
        Threshold17.push(item)

      }
    })
    let Echart_arr = res.map(item => {
      return item = item.dataValues
    })
    let sample_depth = res.map(item => {
      return item = item.dataValues.sample_depth
    })
   


    sample_depth = sample_depth.filter((item, index, self) => {
      return index == self.indexOf(item)
    })//将得到的深度进行去重

    if (assess_type == 1) {

      let depth1 = []
      let depth2 = []
      let depth3 = []
      Echart_arr.forEach((item) => {
        //let in 遍历数组元素即一个包含id,date,point_num还有各种元素值的对象，然后把这些元素对应的值，参考值，时间，是否受污染push到HistoryLists数组中

        for (let key in item)
          if (item["sample_depth"] == sample_depth[0]) {
            delete item.sample_depth;
            for (let key in item) {
              if (item[key] == -1) {
                item[key] = '0'
              }
            }
            depth1.push(item)
          } else if (item["sample_depth"] == sample_depth[1]) {
            delete item.sample_depth;
            for (let key in item) {
              if (item[key] == -1) {
                item[key] = '0'
              }
            }
            depth2.push(item)
          } else if (item["sample_depth"] == sample_depth[2]) {
            delete item.sample_depth;
            for (let key in item) {
              if (item[key] == -1) {
                item[key] = '0'
              }
            }
            depth3.push(item)
          }
      })



      let AllData = []
      let AllRadarData = []
      let all = []
      let arr1 = []
      let arr2 = []
      let arr3 = []
      let arr4 = []
      let arr5 = []
      // AllData.push(Threshold17[0],Threshold18[0],depth1[0],depth2[0],depth3[0])
      if(sample_depth.length == 1){
        AllData.push(Threshold17[0], depth1[0])
      }else{
        AllData.push(Threshold17[0], depth1[0], depth2[0], depth3[0])
      }
      
      if (sample_depth.length == 1) {
        for (let i = 0; i < AllData.length; i++) {
          for (let key in AllData[0]) {


            if (AllData[1][key] == '0' ) {
              //  arr119.push({text:key,max:AllData[0][key]});
              delete AllData[0][key];
              delete AllData[1][key];
              


            }
          }
        }

        let obj_max = []
      for (let key in AllData[0]) {
        obj_max[key] = (AllData[0][key] * 5).toFixed(3)
      }

      AllData.forEach((item) => {
        for (let key in item) {
          if (Number(item[key]) > Number(obj_max[key])) {
            item[key] = obj_max[key]
          }
        }
      })

      for (let key in AllData[0]) {
        if (AllData[0][key] && element_Map.has(key)) {
          if (AllData[0][key] && element_Map.get(key)) {
            AllRadarData.push({ text: element_Map.get(key), max: Number(obj_max[key]) })
          } else {
            AllRadarData.push({ text: element_Map.get(key), max: Number(obj_max[key]) })
          }
        }
        arr1.push(Number(AllData[0][key]));
        arr2.push(Number(AllData[1][key]));
        
      }

      all.push({ max: AllRadarData, Threshold17: arr1, depth1: arr2})
      } else {
        for (let i = 0; i < AllData.length; i++) {
          for (let key in AllData[0]) {


            if (AllData[1][key] == '0' && AllData[2][key] == '0' && AllData[3][key] == '0') {
              //  arr119.push({text:key,max:AllData[0][key]});
              delete AllData[0][key];
              delete AllData[1][key];
              delete AllData[2][key];
              delete AllData[3][key];
              // delete AllData[4][key];
            }
          }
        }
        let obj_max = []
        for (let key in AllData[0]) {
          obj_max[key] = (AllData[0][key] * 5).toFixed(3)
        }
  
        AllData.forEach((item) => {
          for (let key in item) {
            if (Number(item[key]) > Number(obj_max[key])) {
              item[key] = obj_max[key]
            }
          }
        })
  
        for (let key in AllData[0]) {
          if (AllData[0][key] && element_Map.has(key)) {
            if (AllData[0][key] && element_Map.get(key)) {
              AllRadarData.push({ text: element_Map.get(key), max: Number(obj_max[key]) })
            } else {
              AllRadarData.push({ text: element_Map.get(key), max: Number(obj_max[key]) })
            }
          }
          arr1.push(Number(AllData[0][key]));
          arr2.push(Number(AllData[1][key]));
          arr3.push(Number(AllData[2][key]));
          arr4.push(Number(AllData[3][key]));
        }
  
        all.push({ max: AllRadarData, Threshold17: arr1, depth1: arr2, depth2: arr3, depth3: arr4 })

      }


      


      ctx.body = {
        success: true,
        res: all,
        msg: '获取成功'
      }
    } else if (assess_type == 2) {

      let res2 = await AllEchartDataModel.RadarEachDepthValue(assess_type, point_num)
      let Echart_arr2 = res2.map(item => {
        return item = item.dataValues
      })
      let sample_depth = res2.map(item => {
        return item = item.dataValues.sample_depth
      })
      sample_depth = sample_depth.filter((item, index, self) => {
        return index == self.indexOf(item)
      })//将得到的深度进行去重
      let depth1 = []
      let depth2 = []
      let depth3 = []
      let depth4 = []
      Echart_arr2.forEach((item) => {
        delete item.point_num;
        if (item["sample_depth"] == sample_depth[0] && item['statistic_value'] == 'max_value') {
          delete item.statistic_value;
          delete item.sample_depth;
          for (let key in item) {
            if (item[key] == -1) {
              item[key] = '0'
            }
            depth1.push(item)
          }
        } else if (item["sample_depth"] == sample_depth[1] && item['statistic_value'] == 'max_value') {
          delete item.statistic_value;
          delete item.sample_depth;
          for (let key in item) {
            if (item[key] == -1) {
              item[key] = '0'
            }
            depth2.push(item)
          }
        } else if (item["sample_depth"] == sample_depth[2] && item['statistic_value'] == 'max_value') {
          delete item.statistic_value;
          delete item.sample_depth;
          for (let key in item) {
            if (item[key] == -1) {
              item[key] = '0'
            }
            depth3.push(item)

          }
        } else if (item["sample_depth"] == sample_depth[3] && item['statistic_value'] == 'max_value') {
          delete item.statistic_value;
          delete item.sample_depth;
          for (let key in item) {
            if (item[key] == -1) {
              item[key] = '0'
            }
            depth4.push(item)

          }
        }
      })

      let AllData = []
      let AllRadarData = []
      let all = []
      let arr1 = []
      let arr2 = []
      let arr3 = []
      let arr4 = []
      let arr5 = []
      let arr6 = []
      AllData.push(Threshold17[0], depth1[0], depth2[0], depth3[0], depth4[0])


      for (let i = 0; i < AllData.length; i++) {
        for (let key in AllData[0]) {
          if (AllData[1][key] == '0' && AllData[2][key] == '0' && AllData[3][key] == '0' && AllData[4][key] == '0') {
            delete AllData[0][key];
            delete AllData[1][key];
            delete AllData[2][key];
            delete AllData[3][key];
            delete AllData[4][key];
          }
        }
      }
      let obj_max = []
      for (let key in AllData[0]) {
        obj_max[key] = (AllData[0][key] * 5).toFixed(3)

      }
      AllData.forEach((item) => {
        for (let key in item) {
          if (Number(item[key]) > Number(obj_max[key])) {
            item[key] = obj_max[key]
          }
        }
      })
      for (let key in AllData[0]) {
        if (AllData[0][key] && element_Map.has(key)) {
          if (AllData[0][key] && element_Map.get(key)) {
            AllRadarData.push({ text: element_Map.get(key), max: Number(obj_max[key]) })
          } else {
            AllRadarData.push({ text: element_Map.get(key), max: Number(obj_max[key]) })
          }
        }
        arr1.push(Number(AllData[0][key]));
        arr2.push(Number(AllData[1][key]));
        arr3.push(Number(AllData[2][key]));
        arr4.push(Number(AllData[3][key]));
        arr5.push(Number(AllData[4][key]));
      }

      all.push({ max: AllRadarData, Threshold17: arr1, depth1: arr2, depth2: arr3, depth3: arr4, depth4: arr5 })


      ctx.body = {
        success: true,
        res: all,
        msg: '获取成功'
      }
    } else if (assess_type == 3) {
      let res3 = await AllEchartDataModel.RadarEachDepthValue(assess_type, point_num)
      let Echart_arr3 = res3.map(item => {
        return item = item.dataValues
      })
      let type3Data = []

      Echart_arr3.forEach((item) => {
        if (item.statistic_value == 'max_value') {
          delete item.statistic_value;
          delete item.sample_depth;
          type3Data.push(item)

        }
      })

      let all = []
      let arr1 = []
      let arr2 = []
      let arr3 = []
      let AllData = []
      let AllRadarData = []
      AllData.push(Threshold17[0], type3Data[0])

      for (let i = 0; i < AllData.length; i++) {
        for (let key in AllData[0]) {
          if (AllData[1][key] == '0') {
            delete AllData[0][key];
            delete AllData[1][key];
          }
        }
      }
      let obj_max = []
      for (let key in AllData[0]) {
        obj_max[key] = (AllData[0][key] * 5).toFixed(3)

      }

      AllData.forEach((item) => {
        for (let key in item) {
          if (Number(item[key]) > Number(obj_max[key])) {
            item[key] = obj_max[key]
          }
        }
      })

      for (let key in AllData[0]) {
        if (AllData[0][key] && element_Map.has(key)) {
          if (AllData[0][key] && element_Map.get(key)) {
            AllRadarData.push({ text: element_Map.get(key), max: Number(obj_max[key]) })
          } else {
            AllRadarData.push({ text: element_Map.get(key), max: Number(obj_max[key]) })
          }
        }
        arr1.push(Number(AllData[0][key]));
        arr2.push(Number(AllData[1][key]));
      }
      all.push({ max: AllRadarData, Threshold17: arr1, depth1: arr2 })
      ctx.body = {
        success: true,
        res: all,
        msg: '获取成功'
      }

    }

  };

  /**
   *获取ground 3D图的经纬度以及对应传入元素各深度的值
   *
   * @static
   * @param {*} ctx
   * @memberof echart_Controller
   */
  static async getBar3DData(ctx) {
    let SingleElementOptions = ctx.request.body //从前端传入的数据
    let { point_num, assess_type, element } = SingleElementOptions //将传入的数据将进行定义
    let res = await AllEchartDataModel.echartBar3DValue(point_num, assess_type, element)
    let res1 = res.map(item => {
      return item = item.dataValues
    })

    let sample_depth = res.map(item => {
      return item = item.dataValues.sample_depth
    })
    if (assess_type == 1) {
      let depth1 = []
      let depth2 = []
      let depth3 = []
      let data = []
      res1.forEach((item) => {
        if (item.sample_depth == sample_depth[0]) {
          delete item.sample_depth
          for (var key in item) {
            if (item[element] == 0 || item[element] == -1) {
              item[element] = 0
            }
            depth1.push(Number(item[key]))
          }

        } else if (item.sample_depth == sample_depth[1]) {
          delete item.sample_depth
          for (var key in item) {
            if (item[element] == 0 || item[element] == -1) {
              item[element] = 0
            }
            depth2.push(Number(item[key]))
          }
        } else if (item.sample_depth == sample_depth[2]) {
          delete item.sample_depth
          for (var key in item) {
            if (item[element] == 0 || item[element] == -1) {
              item[element] = 0
            }
            depth3.push(Number(item[key]))
          }
        }
      })

      data.push(depth1, depth2, depth3)

      ctx.body = {
        success: true,
        res: data,
        msg: '获取成功'
      }
    } else if (assess_type == 2) {
      let depth1 = []
      let depth2 = []
      let depth3 = []
      let depth4 = []
      let data = []
      let result = []
      let result2 = []
      let result3 = []
      let result4 = []
      var chunk = 3;
      res1.forEach((item) => {
        if (item.sample_depth == sample_depth[0]) {
          delete item.sample_depth
          for (var key in item) {
            if (item[element] == 0 || item[element] == -1) {
              item[element] = 0
            }
            depth1.push(Number(item[key]))
          }
        } else if (item.sample_depth == sample_depth[1]) {
          delete item.sample_depth
          for (var key in item) {
            if (item[element] == 0 || item[element] == -1) {
              item[element] = 0
            }
            depth2.push(Number(item[key]))
          }
        } else if (item.sample_depth == sample_depth[2]) {
          delete item.sample_depth
          for (var key in item) {
            if (item[element] == 0 || item[element] == -1) {
              item[element] = 0
            }
            depth3.push(Number(item[key]))
          }
        } else if (item.sample_depth == sample_depth[3]) {
          delete item.sample_depth
          for (var key in item) {
            if (item[element] == 0 || item[element] == -1) {
              item[element] = 0
            }
            depth4.push(Number(item[key]))
          }
        }
      })

      for (var i = 0, j = depth1.length; i < j; i += chunk) {
        result.push(depth1.slice(i, i + chunk));
      }
      for (var i = 0, j = depth2.length; i < j; i += chunk) {
        result2.push(depth2.slice(i, i + chunk));
      }
      for (var i = 0, j = depth3.length; i < j; i += chunk) {
        result3.push(depth3.slice(i, i + chunk));
      }
      for (var i = 0, j = depth4.length; i < j; i += chunk) {
        result4.push(depth4.slice(i, i + chunk));
      }

      data.push(result, result2, result3, result4)

      ctx.body = {
        success: true,
        res: data,
        msg: '获取成功'
      }
    } else if (assess_type == 3) {

      let b = []

      for (let i of res1) {
        let flag = 0;
        if (b.length == 0) {
          b.push(i)
        }
        if (flag == 0) {
          for (let j of b) {
            if (i['lng'] == j['lng']) {
              delete i.sample_depth
              if (j[element] !== i[element]) {
                j[element] = Number(j[element]) + Number(i[element]);
                j[element] = Number((j[element]).toFixed(2))
              }
              flag = 1;
            }
          }
        } if (flag == 0) {
          delete i.sample_depth
          b.push(i)
        }
      }
      let data = []
      b.forEach((item) => {

        for (let key in item) {
          data.push(item[key])
        }

      })
      var chunk = 3;
      var result = []
      for (var i = 0, j = data.length; i < j; i += chunk) {
        result.push(data.slice(i, i + chunk));
      }

      ctx.body = {
        success: true,
        res: result,
        msg: '获取成功'
      }
    }

  }
}

module.exports = echart_Controller